home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 41 / Amiga Format CD41 (1999-06)(Future Publishing)(GB)[!][issue 1999-07].iso / -seriously_amiga- / programming / other / scm / slib / arraymap.scm < prev    next >
Text File  |  1999-04-19  |  2KB  |  79 lines

  1. ;;;; "arraymap.scm", applicative routines for arrays in Scheme.  
  2. ;;; Copyright (c) 1993 Aubrey Jaffer
  3. ;
  4. ;Permission to copy this software, to redistribute it, and to use it
  5. ;for any purpose is granted, subject to the following restrictions and
  6. ;understandings.
  7. ;
  8. ;1.  Any copy made of this software must include this copyright notice
  9. ;in full.
  10. ;
  11. ;2.  I have made no warrantee or representation that the operation of
  12. ;this software will be error-free, and I am under no obligation to
  13. ;provide any services, by way of maintenance, update, or otherwise.
  14. ;
  15. ;3.  In conjunction with products arising from the use of this
  16. ;material, there shall be no use of my name in any advertising,
  17. ;promotional, or sales literature without prior written consent in
  18. ;each case.
  19.  
  20. (require 'array)
  21.  
  22. (define (array-map! ra0 proc . ras)
  23.   (define (ramap rshape inds)
  24.     (if (null? (cdr rshape))
  25.     (do ((i (cadar rshape) (+ -1 i))
  26.          (is (cons (cadar rshape) inds)
  27.          (cons (+ -1 i) inds)))
  28.         ((< i (caar rshape)))
  29.       (apply array-set! ra0
  30.          (apply proc (map (lambda (ra) (apply array-ref ra is))
  31.                   ras))
  32.          is))
  33.     (let ((crshape (cdr rshape))
  34.           (ll (caar rshape)))
  35.       (do ((i (cadar rshape) (+ -1 i)))
  36.           ((< i ll))
  37.         (ramap crshape (cons i inds))))))
  38.   (ramap (reverse (array-shape ra0)) '()))
  39.  
  40. (define (array-for-each proc . ras)
  41.   (define (rafe rshape inds)
  42.     (if (null? (cdr rshape))
  43.     (do ((i (caar rshape) (+ 1 i)))
  44.         ((> i (cadar rshape)))
  45.       (apply proc
  46.          (map (lambda (ra)
  47.             (apply array-ref ra (reverse (cons i inds)))) ras)))
  48.     (let ((crshape (cdr rshape))
  49.           (ll (cadar rshape)))
  50.       (do ((i (caar rshape) (+ 1 i)))
  51.           ((> i ll))
  52.         (rafe crshape (cons i inds))))))
  53.   (rafe (array-shape (car ras)) '()))
  54.  
  55. (define (array-index-map! ra fun)
  56.   (define (ramap rshape inds)
  57.     (if (null? (cdr rshape))
  58.     (do ((i (cadar rshape) (+ -1 i))
  59.          (is (cons (cadar rshape) inds)
  60.          (cons (+ -1 i) inds)))
  61.         ((< i (caar rshape)))
  62.       (apply array-set! ra (apply fun is) is))
  63.     (let ((crshape (cdr rshape))
  64.           (ll (caar rshape)))
  65.       (do ((i (cadar rshape) (+ -1 i)))
  66.           ((< i ll))
  67.         (ramap crshape (cons i inds))))))
  68.   (if (zero? (array-rank ra))
  69.       (array-set! ra (fun))
  70.       (ramap (reverse (array-shape ra)) '())))
  71.  
  72. (define (array-indexes ra)
  73.   (let ((ra0 (apply make-array '() (array-shape ra))))
  74.     (array-index-map! ra0 list)
  75.     ra0))
  76.  
  77. (define (array-copy! source dest)
  78.   (array-map! dest identity source))
  79.